package admin

import (
	"context"

	"open/pkg/middleware/auth"

	"github.com/go-kratos/kratos/v2/errors"
	"gorm.io/gorm"

	v1 "open/api/open/admin/v1"
	"open/internal/types"
)

// ListWechatMiniVersionLogs 获取微信小程序发布记录
func (s *Service) ListWechatMiniVersionLogs(ctx context.Context, req *v1.ListWechatMiniVersionLogsRequest) (*v1.ListWechatMiniVersionLogsReply, error) {
	user, err := auth.FromContext(ctx)
	if err != nil {
		return nil, err
	}
	appID, err := s.dao.GetAppIDByBrandID(ctx, user.BrandID, types.Wechat)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.BadRequest("APP_NOT_FOUND", "品牌微信小程序不存在")
		}
		return nil, err
	}
	appVersion, err := s.dao.GetAppVersion(ctx, appID, types.Wechat)
	if err != nil {
		return nil, err
	}
	versionLogs, err := s.dao.VersionLogs(ctx, appVersion.ID, req.PageNum, req.PageSize)
	if err != nil {
		return nil, err
	}
	records := make([]*v1.ListWechatMiniVersionLogsReply_Record, len(versionLogs))
	for i, r := range versionLogs {
		records[i] = &v1.ListWechatMiniVersionLogsReply_Record{
			Version:    r.Version,
			UpdateTime: r.UpdatedAt.String(),
		}
	}
	return &v1.ListWechatMiniVersionLogsReply{
		Total:   0,
		Records: records,
	}, nil
}

// ListAlipayMiniVersionLogs 获取支付宝小程序发布记录
func (s *Service) ListAlipayMiniVersionLogs(ctx context.Context, req *v1.ListAlipayMiniVersionLogsRequest) (*v1.ListAlipayMiniVersionLogsReply, error) {
	user, err := auth.FromContext(ctx)
	if err != nil {
		return nil, err
	}
	appID, err := s.dao.GetAppIDByBrandID(ctx, user.BrandID, types.Alipay)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.BadRequest("APP_NOT_FOUND", "支付宝小程序不存在")
		}
		return nil, err
	}
	appVersion, err := s.dao.GetAppVersion(ctx, appID, types.Alipay)
	if err != nil {
		return nil, err
	}
	versionLogs, err := s.dao.VersionLogs(ctx, appVersion.ID, req.PageNum, req.PageSize)
	if err != nil {
		return nil, err
	}
	records := make([]*v1.ListAlipayMiniVersionLogsReply_Record, len(versionLogs))
	for i, r := range versionLogs {
		records[i] = &v1.ListAlipayMiniVersionLogsReply_Record{
			Version:    r.Version,
			UpdateTime: r.UpdatedAt.String(),
		}
	}
	return &v1.ListAlipayMiniVersionLogsReply{
		Total:   0,
		Records: records,
	}, nil
}

// ListBytedanceMiniVersionLogs 获取字节跳动小程序发布记录
func (s *Service) ListBytedanceMiniVersionLogs(ctx context.Context, req *v1.ListBytedanceMiniVersionLogsRequest) (*v1.ListBytedanceMiniVersionLogsReply, error) {
	user, err := auth.FromContext(ctx)
	if err != nil {
		return nil, err
	}
	appID, err := s.dao.GetAppIDByBrandID(ctx, user.BrandID, types.ByteDance)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.BadRequest("APP_NOT_FOUND", "字节跳动小程序不存在")
		}
		return nil, err
	}
	appVersion, err := s.dao.GetAppVersion(ctx, appID, types.ByteDance)
	if err != nil {
		return nil, err
	}
	versionLogs, err := s.dao.VersionLogs(ctx, appVersion.ID, req.PageNum, req.PageSize)
	if err != nil {
		return nil, err
	}
	records := make([]*v1.ListBytedanceMiniVersionLogsReply_Record, len(versionLogs))
	for i, r := range versionLogs {
		records[i] = &v1.ListBytedanceMiniVersionLogsReply_Record{
			Version:    r.Version,
			UpdateTime: r.UpdatedAt.String(),
		}
	}
	return &v1.ListBytedanceMiniVersionLogsReply{
		Total:   0,
		Records: records,
	}, nil
}
